﻿using Microsoft.EntityFrameworkCore;
using Scheduler.IServices;
using Scheduler.Model;

namespace Scheduler.Services
{
    /// <summary>
    /// RoleModulePermissionServices 应用服务
    /// </summary>	
    public class RoleModulePermissionServices : IRoleModulePermissionServices
    {
        private readonly SchedulerContext dbContext;
        public RoleModulePermissionServices(SchedulerContext schedulerContext)
        {
            dbContext=schedulerContext;
        }

        /// <summary>
        /// 获取全部 角色接口(按钮)关系数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<RoleModulePermission>> GetRoleModule()
        {
            var roleModulePermissions = await dbContext.RoleModulePermissions.Where(a => a.IsDeleted == false).ToListAsync();
            var roles = await dbContext.Roles.Where(a => a.IsDeleted == false).ToListAsync();
            var modules = await dbContext.Modules.Where(a => a.IsDeleted == false).ToListAsync();
            if (modules is null || modules.Count == 0)
            {
                return roleModulePermissions;
            }
            if (roleModulePermissions.Count > 0)
            {
                foreach (var item in roleModulePermissions)
                {
                    if (roles is not null)
                    {
                        item.Role = roles.FirstOrDefault(d => d.Id == item.RoleId);
                    }
                    if (modules is not null)
                    {
                        item.Module = modules.FirstOrDefault(d => d.Id == item.ModuleId);
                    }
                }
            }
            return roleModulePermissions;
        }

        /// <summary>
        /// 角色权限Map
        /// RoleModulePermission, Module, Role 三表联合
        /// 第四个类型 RoleModulePermission 是返回值
        /// </summary>
        /// <returns></returns>
        public async Task<List<RoleModulePermission>> RoleModuleMaps()
        {
            var data = from rmp in dbContext.RoleModulePermissions
                       where !rmp.IsDeleted
                       join m in dbContext.Modules.Where(m => !m.IsDeleted)
                       on rmp.ModuleId equals m.Id into mj
                       from m in mj.DefaultIfEmpty()
                       join r in dbContext.Roles.Where(r => !r.IsDeleted)
                       on rmp.RoleId equals r.Id into rj
                       from r in rj.DefaultIfEmpty()
                       select new RoleModulePermission
                       {
                           Role = r,
                           Module = m,
                           IsDeleted = rmp.IsDeleted
                       };
            return await data.ToListAsync();
        }
        /// <summary>
        /// 登录角色权限Map
        /// </summary>
        /// <returns></returns>
        public async Task<List<PermissionItem>> LoginRoleModuleMaps()
        {
            var data = from rmp in dbContext.RoleModulePermissions
                       join m in dbContext.Modules on rmp.ModuleId equals m.Id into mj
                       from m in mj.DefaultIfEmpty()
                       join r in dbContext.Roles on rmp.RoleId equals r.Id into rj
                       from r in rj.DefaultIfEmpty()
                       where !rmp.IsDeleted && (m == null || !m.IsDeleted) && (r == null || !r.IsDeleted)
                       select new PermissionItem
                       {
                           Role = r.Name,
                           Url = m.LinkUrl
                       };
            return await data.ToListAsync();
        }

        /// <summary>
        /// 查询出角色-菜单-接口关系表全部Map属性数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<RoleModulePermission>> GetRMPMaps()
        {
            var data = await dbContext.RoleModulePermissions
                .Include(rmp => rmp.Module)     
                .Include(rmp => rmp.Permission)  
                .Include(rmp => rmp.Role)        
                .Where(rmp => !rmp.IsDeleted)   
                .ToListAsync();
            return data;
        }   
    }
}
